package com.lzw.DailyAudit.service.dateAggregation_02.DailyAudit.service.generateInvoices_03;


import com.hooya.fa.eu.biz.DailyAudit.entity.DailyAuditStatus;
import com.hooya.fa.eu.biz.DailyAudit.entity.InvInvoice;
import com.hooya.fa.eu.biz.DailyAudit.entity.InvInvoiceDtl;
import com.hooya.fa.eu.biz.DailyAudit.entity.dto.InvoiceDataDTO;
import com.hooya.fa.eu.biz.DailyAudit.mapper.*;
import com.hooya.fa.eu.biz.DailyAudit.service.dateAggregation_02.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class Insert_Transfer_Invoice {

    @Autowired
    private DailyStkOrigDataCollectMapper stkOrigdataCollectMapper;

    @Autowired
    private Insert_Add_Invoice insert_add_invoice;

	@Autowired
	private DailyAuditStatusMapper statusMapper;

	@Autowired
	private CommonService commonService;

	/**
	 * 插入转运发票方法
	 */
	@Transactional
	public void insert_transfer_invoice(String currentDate) {
		try {

			String name = "insert_transfer_invoice-03";
			DailyAuditStatus dailyAuditStatus = statusMapper.selectStatus(name, currentDate);
			if(dailyAuditStatus != null && dailyAuditStatus.getFlag() == 1){
				log.info("{} - 该方法已执行，进行跳过！",name);
				return;
			}

			log.info("开始执行插入转运发票步骤，开始日期：{}", currentDate);

            // 查询所有库存数据
            List<InvoiceDataDTO> origGenerateData = stkOrigdataCollectMapper.getTranGenerateData(currentDate);
            origGenerateData.forEach(f -> {
                f.setFullseller(f.getFullseller().toLowerCase());
                f.setFullbuyer(f.getFullbuyer().toLowerCase());
            });
            List<InvoiceDataDTO> origDetailData = stkOrigdataCollectMapper.getTranDetailData(currentDate);


            // 获取税号数据 -- 复用add
            List<InvoiceDataDTO> taxIdData = insert_add_invoice.getTaxid();
            // 生成发票明细 -- 复用add
            List<InvoiceDataDTO> invoiceDtl = insert_add_invoice.generateInvoiceDtl(origDetailData);
            // 添加信息 -- 复用add
            List<InvoiceDataDTO> detailData = insert_add_invoice.addInfos(invoiceDtl);
            // 添加税号 -- 复用add
            List<InvoiceDataDTO> generateData = insert_add_invoice.addTaxid(origGenerateData,taxIdData);


            // 转换为实体类并保存
            List<InvInvoice> invInvoices = insert_add_invoice.convertToInvInvoice(generateData);
            List<InvInvoiceDtl> invInvoiceDtls = insert_add_invoice.convertToInvInvoiceDtl(detailData);
            // 批量插入数据
            insert_add_invoice.batchInsertInvoices(invInvoices);
            insert_add_invoice.batchInsertInvoiceDtls(invInvoiceDtls);

			// 修改执行状态
			commonService.UpdateOrInsertStatus(dailyAuditStatus, name, currentDate);

			log.info("插入转运发票步骤执行完成");
        } catch (Exception e) {
            log.error("插入转运发票步骤执行失败", e);
            throw new RuntimeException("插入转运发票步骤执行失败", e);
        }
    }

}